#!/bin/bash
### BEGIN INIT INFO
# Provides:          catnip
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Catnip Development Server
# Description:       Catnip development environment server
### END INIT INFO

PATH=/opt/catnip/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
DESC="Catnip Development Server"
NAME=catnip
DAEMON=/opt/catnip/bin/catnip-run.sh
PIDFILE=/opt/catnip/catnip.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Function that starts the daemon/service
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid vscode:vscode --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid vscode:vscode --exec $DAEMON
    RETVAL="$?"
    return "$RETVAL"
}

# Function that stops the daemon/service
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped

    # Try to stop by pidfile first
    if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid vscode:vscode
        RETVAL="$?"
    else
        # Fallback: stop by process name/exec
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --exec $DAEMON --chuid vscode:vscode
        RETVAL="$?"
    fi

    [ "$RETVAL" = 2 ] && return 2
    rm -f $PIDFILE
    return "$RETVAL"
}

# Function that reloads the daemon/service
do_reload() {
    # Just restart - catnip doesn't support reload
    do_stop
    do_start
}

case "$1" in
  start)
    echo "Starting $DESC" "$NAME"
    do_start
    case "$?" in
        0|1) echo "   ...done." ;;
        2) echo "   ...failed." ;;
    esac
    ;;
  stop)
    echo "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
        0|1) echo "   ...done." ;;
        2) echo "   ...failed." ;;
    esac
    ;;
  status)
    if [ -f $PIDFILE ]; then
        if kill -0 $(cat $PIDFILE) 2>/dev/null; then
            echo "$NAME is running"
            exit 0
        else
            echo "$NAME is not running (stale pidfile)"
            exit 1
        fi
    else
        echo "$NAME is not running"
        exit 3
    fi
    ;;
  reload|force-reload)
    echo "Reloading $DESC" "$NAME"
    do_reload
    ;;
  restart)
    echo "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) echo "   ...done." ;;
            1) echo "   ...already running." ;;
            *) echo "   ...failed to start." ;;
        esac
        ;;
      *)
        echo "   ...failed to stop."
        ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
    exit 3
    ;;
esac

: